extends CharacterBody2D

@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
# 获取输入方向
var direction = Vector2.ZERO

func _physics_process(delta: float) -> void:
	# 如果没有按下任何键，停止移动
	if !Input.is_anything_pressed():
		direction = Vector2.ZERO
	if Input.is_action_pressed("move_up"):
		direction.y -= 1
	if Input.is_action_pressed("move_down"):
		direction.y += 1
	if Input.is_action_pressed("move_left"):
		direction.x -= 1
	if Input.is_action_pressed("move_right"):
		direction.x += 1

	#print(direction)

	if direction.x > 0:
		animated_sprite.flip_h = false
	elif direction.x < 0:
		animated_sprite.flip_h = true

	if direction == Vector2.ZERO:
		animated_sprite.play("idle")
	else:
		animated_sprite.play("move")

	# 标准化方向向量，确保斜向移动速度一致
	direction = direction.normalized()
	velocity = direction * SPEED
	
	#移动角色
	move_and_slide()
